const Controller = require('egg').Controller;

class StudentController extends Controller {
  async getStudentList() {
    const { ctx } = this;
    ctx.validate({
      class_id: { type: 'string', required: true }
    }, ctx.query)
    const params = {
      class_id: Number(ctx.query.class_id),
      account_id : ctx.state.user.id
    }
    const { data } = await ctx.service.student.getStudentList(params)
    ctx.body = {
      code: 200,
      msg: 'Get student list success',
      data: data || []
    }
  }

  async addStudent() {
    const { ctx } = this;
    ctx.validate({
      class_id: { type: 'number' },  
      stu_name: { type:'string'}
    })
    const params = ctx.request.body
    params.account_id = ctx.state.user.id
    const { error } = await ctx.service.student.addStudent(params)
    const resBody = {
      code: 200,
      msg: 'Add classes success',
    };
    if (error) {
      resBody.code = -1;
      resBody.msg = error
    }

    ctx.body = resBody;
  }

    async updateStudent() {
    const { ctx } = this;
    ctx.validate({
      class_id: {type: 'number' },  
      stu_name: { type:'string'}
    })
    const params = ctx.request.body
    params.account_id = ctx.state.user.id
    const { error } = await ctx.service.student.updateStudent(params)
    const resBody = {
      code: 200,
      msg: 'Update classes success',
    };
    if (error) {
      resBody.code = -1;
      resBody.msg = error
    }

    ctx.body = resBody;
  }

  async delStudent() {
    const { ctx } = this;
    ctx.validate({
      stu_id_list: { type: 'array' }
    });
    const param = ctx.request.body;
    param.account_id = ctx.state.user.id
    const { error } = await ctx.service.student.delStudent(param);
    const response = {
      code: 200,
      msg: 'Delete student success',
    };
    if (error) {
      response.code = -1;
      response.msg = error;
    }
    ctx.body = response;
  }

  async batchAddStudent() {
    const { ctx } = this;
    ctx.validate({
      class_id: { type: 'number' },
      stu_list: { type: 'array'}
    });
    const param = ctx.request.body;
    param.account_id = ctx.state.user.id
    const { error } = await ctx.service.student.batchAddStudent(param);
    const response = {
      code: 200,
      msg: 'Batch add student success',
    };
    if (error) {
      response.code = -1;
      response.msg = error;
    }
    ctx.body = response;
  }

  async batchExcelStudent() {
    const { ctx } = this;
    ctx.validate({
      stu_list: { type: 'array' },
    });
    const param = ctx.request.body;
    param.account_id = ctx.state.user.id
    const { error } = await ctx.service.student.batchExcelAddStudent(param);
    const response = {
      code: 200,
      msg: 'Batch add student success',
    };
    if (error) {
      response.code = -1;
      response.msg = error;
    }
    ctx.body = response;
  }
}

module.exports = StudentController;
